package com.neusoft.realsight.apm.dao;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * Created by Administrator on 2016/6/10.
 */
public class ExcelDao {
    public static  XSSFRow tempRow;
    public static XSSFWorkbook createHSSFWorkBook(){
    	XSSFWorkbook hssfwb = new XSSFWorkbook();
        return hssfwb;
    }
    public static XSSFSheet createSheet(XSSFWorkbook wb,String name){
        return wb.createSheet(name);
    }
    public static void saveHSSFWorkBook(XSSFWorkbook wb ,String path){
        FileOutputStream fos = null;
        try {
             fos = new FileOutputStream(path);
            wb.write(fos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fos != null){
                try {
                    fos.close();
                    wb.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

    }

    /**
     *
     * @param content
     * @param sheet
     * @param row if row is null,it will be created
     * @param r
     * @param c
     * @param cellStyle
     */
    public static void addContentInCell(String content, XSSFSheet sheet, XSSFRow row, int r, int c, HSSFCellStyle cellStyle){
        if (tempRow != null &&tempRow.getRowNum() == r){row = tempRow;}
        else if (row == null) row = sheet.createRow(r);
        XSSFCell hssfCell = row.createCell(c);
        hssfCell.setCellValue(content);
        if (cellStyle !=null){
            hssfCell.setCellStyle(cellStyle);
        }
        tempRow = row;
    }
}
